 package server;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;







 public class RankingTop
 {
/*  25 */   private static final Logger log = LogManager.getLogger(RankingTop.class.getName());
/*  26 */   private static final RankingTop instance = new RankingTop();
/*  27 */   private final Map<String, List<CharNameAndId>> rankcache = new HashMap<>();

   private RankingTop() {
/*  30 */     log.info("正在启动[排行榜]");
/*  31 */     initAll();
   }



   public static RankingTop getInstance() {
/*  37 */     return instance;
   }

   public final void initAll() {
/*  41 */     this.rankcache.clear();
   }

   public final List<CharNameAndId> getRanking(String rankingname) {
/*  45 */     return getRanking(rankingname, 10);
   }

   public final List<CharNameAndId> getRanking(String rankingname, int previous) {
/*  49 */     return getRanking(rankingname, 10, true);
   }

   public final List<CharNameAndId> getRanking(String rankingname, int previous, boolean repeatable) {
/*  53 */     List<CharNameAndId> ret = null;
/*  54 */     if (!this.rankcache.containsKey(rankingname)) {
/*  55 */       try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
         ResultSet rs;
/*  57 */         try (PreparedStatement ps = con.prepareStatement("SELECT r.accountid, r.characterid, r.rankingname, r.value, r.time, c.name, c.gender FROM (SELECT * FROM rankingtop ORDER BY value DESC) r, characters AS c WHERE r.characterid = c.id AND r.rankingname = ? " + (repeatable ? "" : "GROUP BY r.characterid") + " ORDER BY r.value DESC, r.time DESC LIMIT ?;")) {
/*  58 */           ps.setString(1, rankingname);
/*  59 */           ps.setInt(2, previous);
/*  60 */           rs = ps.executeQuery();
/*  61 */           ret = new LinkedList<>();
/*  62 */           while (rs.next()) {
/*  63 */             ret.add(new CharNameAndId(rs.getInt("accountid"), rs.getInt("characterid"), rs.getString("rankingname"), rs.getInt("value"), rs.getTimestamp("time"), rs.getString("name"), rs.getInt("gender")));
           }
         }
/*  66 */         rs.close();
/*  67 */       } catch (SQLException ex) {
/*  68 */         log.error(ex);
       }
/*  70 */       this.rankcache.put(rankingname, ret);
     } else {
/*  72 */       return this.rankcache.get(rankingname);
     }
/*  74 */     return ret;
   }

   public final void insertRanking(MapleCharacter player, String rankingname, int value) {
/*  78 */     try(DruidPooledConnection con = DatabaseConnection.getInstance().getConnection(); 
/*  79 */         PreparedStatement ps = con.prepareStatement("INSERT INTO rankingtop (id, accountid, characterid, rankingname, value, time) VALUES (DEFAULT, ?, ?, ?, ?, CURRENT_TIMESTAMP)")) {
/*  80 */       ps.setInt(1, player.getAccountID());
/*  81 */       ps.setInt(2, player.getId());
/*  82 */       ps.setString(3, rankingname);
/*  83 */       ps.setInt(4, value);
/*  84 */       ps.executeUpdate();
     }
/*  86 */     catch (SQLException ex) {
/*  87 */       log.error(ex);
     }
   }


   public static final class CharNameAndId
   {
     public final String name;
     public final String rankingname;
     public final int accountid;

     public CharNameAndId(int accountid, int characterid, String rankingname, int value, Timestamp time, String name, int gender) {
/*  99 */       this.accountid = accountid;
/* 100 */       this.characterid = characterid;
/* 101 */       this.rankingname = rankingname;
/* 102 */       this.value = value;
/* 103 */       this.time = time;
/* 104 */       this.name = name;
/* 105 */       this.gender = gender;
     }

     public final int characterid;
     public final int value;
     public final int gender;
     public final Timestamp time;
   }
 }


/* Location:              C:\PGX\Server.jar!\server\RankingTop.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */